package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.AlertRuleDto;
import com.zzyl.entity.AlertRule;
import com.zzyl.entity.User;
import com.zzyl.mapper.AlertRuleMapper;
import com.zzyl.service.AlertRuleServicr;
import com.zzyl.service.DeptService;
import com.zzyl.service.DeviceService;
import com.zzyl.utils.UserThreadLocal;
import com.zzyl.vo.AlertRuleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AlertRuleServicrImpl implements AlertRuleServicr {

    @Autowired
    private AlertRuleMapper alertRuleMapper;

    @Autowired
    private DeviceService deviceService;

    /**
     * 新增报警规则
     * @param alertRuleDto
     */
    @Override
    public void create(AlertRuleDto alertRuleDto) {
        AlertRule alertRule = new AlertRule();
        alertRule.setCreateBy(UserThreadLocal.get());
        BeanUtil.copyProperties(alertRuleDto,alertRule);
        alertRuleMapper.create(alertRule);
    }


    /**
     * 报警规则分页
     * @param pageNum
     * @param pageSize
     * @param alertRuleName
     * @param functionName
     * @param productKey
     * @return
     */
    @Override
    public PageResponse<AlertRuleVo> getPage(Integer pageNum, Integer pageSize,
                                             String alertRuleName, String functionName,
                                             String productKey) {
        PageHelper.startPage(pageNum,pageSize);
        Page<AlertRuleVo> alertRules =alertRuleMapper.getPage(alertRuleName,functionName,productKey);
        alertRules.getResult().forEach(alertRule -> {
            alertRule.setRules(alertRule.getFunctionName()+alertRule.getOperator()+alertRule.getValue()+"，持续"+alertRule.getDuration()+"个周期");
        });
        return PageResponse.of(alertRules, AlertRuleVo.class);
    }


    @Override
    public AlertRuleVo read(Integer id) {
        AlertRuleVo alertRuleVo=alertRuleMapper.read(id);
        return alertRuleVo;
    }

    @Override
    public void update(AlertRuleDto alertRuleDto, Integer id) {
        AlertRule alertRule = BeanUtil.toBean(alertRuleDto, AlertRule.class);
        alertRuleMapper.update(alertRule,id);
    }

    @Override
    public void delete(Integer id) {
        alertRuleMapper.delete(id);
    }

    @Override
    public void updateStatus(Integer id, Integer status) {
        alertRuleMapper.updateStatus(id,status);
    }
}
